package servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Bizlogic.CAManager;
import DAO.DAOException;
import DAO.ProductsDAO;
import DAO.MySQL.ProductsDAOImpl;
import Model.Employee;
import Model.Product;

/**
 * @author TONY
 *
 */
public class ListParts extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
         CAManager ca= new CAManager();
		// ProductsDAO pd=new ProductsDAOImpl();
			List<Product> list=null;
			list=ca.findAllProducts();
			request.setAttribute("products", list);
			HttpSession session=request.getSession();
			Employee employee=(Employee)session.getAttribute("employee");
			if(employee.getRoleid()==1)
			request.getRequestDispatcher("ListProduct.jsp").forward(request, response);
			else
				request.getRequestDispatcher("MeListProduct.jsp").forward(request, response);	
			
	}
	
	

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		
		 ProductsDAO pd=new ProductsDAOImpl();
			List<Product> list=null;
			try {
				list=pd.findAllProduct();
			} catch (DAOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			request.setAttribute("products", list);
			HttpSession session=request.getSession();
			Employee employee=(Employee)session.getAttribute("employee");
			if(employee.getRoleid()==1)
			request.getRequestDispatcher("ListProduct.jsp").forward(request, response);
			else
				request.getRequestDispatcher("MeListProduct.jsp").forward(request, response);	
	}

}
